import { mapState, mapMutations } from 'vuex'

// 播放器
export const playListMixin = {
  computed: {
    ...mapState(['playList'])
  },
  mounted () {
    this.handlePlaylist(this.playList)
  },
  activated () {
    this.handlePlaylist(this.playList)
  },
  watch: {
    playList (newAal) {
      this.handlePlaylist(newAal)
    }
  },
  methods: {
    handlePlaylist () {
      throw new Error('component must  implement handlePlay methods')
    }
  }
}

// 收藏歌曲
export const playerMixin = {
  computed: {
    ...mapState(['likeMusic', 'currentSong'])
  },
  methods: {
    ...mapMutations(['setCurrentIndex']),
    toggleFavorite (song) {
      const index = this.isFavorite(song)
      if (index > -1) {
        // 已有这首收藏歌曲，不再收藏
        this.likeMusic.splice(index, 1)
      } else {
        // 这首歌没有收藏
        this.likeMusic.unshift(song)
      }
      localStorage.setItem('likeMusic', JSON.stringify(this.likeMusic))
    },
    getFavoriteIcon (song) {
      const index = this.isFavorite(song)
      // 存在时的icon样式
      if (index > -1) {
        return 'icon-favorite'
      // 不存在时的icon样式
      } else {
        return 'icon-not-favorite'
      }
    },
    // 判断是否收藏歌曲
    isFavorite (song) {
      const index = this.likeMusic.findIndex(item => {
        return item.mid === song.mid
      })
      // 有收藏歌曲,索引
      return index
    },
    // 本地存储
    local (arr, name) {
      arr.forEach((item, index) => {
        if (item.mid === this.currentSong.mid) {
          arr.splice(index, 1)
        }
      })
      arr.unshift(this.currentSong)
      localStorage.setItem(name, JSON.stringify(arr))
    },
    resetCurrentIndex (list) {
      // 找到当前歌曲在新列表中的索引，防止切换播放顺序时会自动切歌
      let index = list.findIndex((item) => {
        return item.mid === this.currentSong.mid
      })
      this.setCurrentIndex(index)
    }
  }
}
// 收藏歌手
export const singerMixin = {
  computed: {
    ...mapState(['likeSinger'])
  },
  methods: {
    toggleFavorite (singer) {
      const index = this.isFavorite(singer)
      if (index > -1) {
        // 已关注歌手
        this.likeSinger.splice(index, 1)
      } else {
        // 未收藏歌手
        this.likeSinger.unshift(singer)
      }
      localStorage.setItem('likeSinger', JSON.stringify(this.likeSinger))
    },
    getFavoriteText (singer) {
      const index = this.isFavorite(singer)
      if (index > -1) {
        return '已关注'
      } else {
        return '关注'
      }
    },
    // 判断是否收藏歌曲
    isFavorite (singer) {
      const index = this.likeSinger.findIndex((item) => {
        return item.singer_id === singer.singer_id
      })
      // 有收藏歌曲,索引
      return index
    }
  }
}
export const listMixin = {
  computed: {
    ...mapState([
      'type',
      'likeSongList',
      'likeAlbum',
      'likeRank',
      'likeSinger',
      'songList',
      'album',
      'rank',
      'singer'
    ])
  },
  methods: {
    toggleFavorite () {
      // 确定索引
      const index = this.isFavorite() // 0
      let likeList, item, likeLocal
      const type = this.type
      // console.log(type)
      if (type === '歌单') {
        likeList = this.likeSongList
        item = this.songList
        likeLocal = 'likeSongList'
      }
      if (type === '歌手') {
        likeList = this.likeSinger
        item = this.singer
        likeLocal = 'likeSinger'
      }
      if (type === '排行') {
        likeList = this.likeRank
        item = this.rank
        likeLocal = 'likeRank'
      }
      if (type === '专辑') {
        likeList = this.likeAlbum
        item = this.album
        likeLocal = 'likeAlbum'
      }
      if (index > -1) {
        // 已有这首收藏歌单，不再收藏
        likeList.splice(index, 1)
      } else { // 歌单没有收藏
        likeList.unshift(item)
      }
      localStorage.setItem(likeLocal, JSON.stringify(likeList))
    },
    getFavoriteIcon () {
      const index = this.isFavorite()
      // console.log(index)
      if (index > -1) {
        return 'icon-favorite'
      } else {
        return 'icon-not-favorite'
      }
    },
    // 判断是否收藏歌曲
    isFavorite () {
      let index
      if (this.type === '歌单') {
        index = this.likeSongList.findIndex((item) => { return item.content_id === this.songList.content_id })
        return index
      }
      if (this.type === '排行') {
        index = this.likeRank.findIndex((item) => {
          return item.topId === this.rank.topId
        })
        return index
      }
      if (this.type === '专辑') {
        index = this.likeAlbum.findIndex((item) => {
          return item.albumMID === this.album.albumMID
        })
        return index
      }
      if (this.type === '歌手') {
        index = this.likeSinger.findIndex((item) => {
          return item.singer_id === this.singer.singer_id
        })
        return index
      }
    }
  }
}
